package com.spontaneappnew.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RatingBar;
import android.widget.TextView;
import com.spontaneappnew.Place;
import com.spontaneappnew.R;

public class AddedPlacesAdapter extends BaseExpandableListAdapter {
	
	private LayoutInflater li;
	private ArrayList<Place> places;

	
	public AddedPlacesAdapter(Context context, ArrayList<Place> PList){
		   this.places = PList;
		   this.li = LayoutInflater.from(context);
	}
	
	@Override
	public Place getChild(int groupPosition, int childPosition) {
		return this.places.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
		final Place place = (Place) getChild(groupPosition, childPosition);
		if (convertView == null) {
			convertView = li.inflate(R.layout.added_place_row, null);
		}
		TextView name = (TextView) convertView.findViewById(R.id.added_place_row_description);
		name.setText(place.getDescription());
		TextView address = (TextView) convertView.findViewById(R.id.added_place_row_address);
		address.setText(place.getStreet()+"\n"+ place.getCity()+"\n"+place.getCountry() );
		RatingBar rating  = (RatingBar) convertView.findViewById(R.id.added_place_row_rating);
		rating.setRating(place.getRating());
		TextView date = (TextView) convertView.findViewById(R.id.added_place_row_date);
		date.setText(DateUtils.getRelativeTimeSpanString(place.getDate().getTime()).toString());
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Place getGroup(int groupPosition) {
		return this.places.get(groupPosition);
	}
	
	@Override
	public int getGroupCount() {
		if(places != null)
			return places.size();
		return 0;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
		String headerTitle = getGroup(groupPosition).getName();
		if (convertView == null) {
				convertView = li.inflate(R.layout.added_places_header, null);
		}
	    TextView headerName = (TextView) convertView.findViewById(R.id.added_places_header_textview);
	    headerName.setTypeface(null, Typeface.BOLD);
	    headerName.setText(headerTitle);

	    return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
